package com.tiny.pong;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import com.tiny.core.game.GameObject;
import com.tiny.core.game.states.GameState;
import com.tiny.core.game.states.StateBasedGame;
import com.tiny.core.input.KeyboardManager;
import com.tiny.core.physics.CollisionDetector;
import com.tiny.core.physics.Vector;
import com.tiny.utils.GameObjectManager;

public class Game implements GameState {

    private List<GameObject> objects;
    private CollisionDetector collisor;
    private GameObjectManager objectManager;

    private int score1;
    private int score2;

    private Ball ball;

    private Font f;

    @Override
    public void onLoad(StateBasedGame game) {
        f = new Font("arial", Font.BOLD, 56);
        score1 = 0;
        score2 = 0;
        objects = new ArrayList<GameObject>();
        collisor = new CollisionDetector(objects);
        objectManager = new GameObjectManager(objects);

        objectManager.add(new StaticWall(0, 30, game.getWidth(), 10));
        objectManager.add(new StaticWall(0, game.getHeight() - 30, game.getWidth(), 10));

        int playerCenter = (game.getHeight() - 200) / 2;

        Player player1 = new Player(10, playerCenter, 10, 200, KeyEvent.VK_Q, KeyEvent.VK_A, game.getMovimentCalculator());
        objectManager.add(player1);

        Player player2 = new Player(game.getWidth() - 20, playerCenter, 10, 200, KeyEvent.VK_UP, KeyEvent.VK_DOWN, game.getMovimentCalculator());
        objectManager.add(player2);

        createBall(game);
    }

    private void createBall(StateBasedGame game) {
        int ballCenterX = (game.getWidth() - 40) / 2;
        int ballCenterY = (game.getHeight() - 40) / 2;
        ball = new Ball(ballCenterX, ballCenterY, 40, 40, game.getMovimentCalculator());
        ball.setVelocity(new Vector(150d, 150d));
        objectManager.add(ball);
    }

    @Override
    public void onRender(Graphics2D g, StateBasedGame game) {
        for (GameObject o : objects) {
            o.render(g);
        }

        g.setFont(f);
        g.setColor(Color.DARK_GRAY);
        g.drawString(String.valueOf(score1), 10, 90);
        g.drawString(String.valueOf(score2), game.getWidth() - 56, 90);
    }

    @Override
    public void onUnload(StateBasedGame game) {

    }

    @Override
    public void onUpdate(int currentTick, StateBasedGame game) {
        collisor.update();
        objectManager.update(currentTick);

        if (ball.getPos().x + ball.getPos().width < 0) {
            score2++;
            ball.setEnergy(0);
            createBall(game);
        } else if (ball.getPos().x > game.getWidth()) {
            score1++;
            ball.setEnergy(0);
            createBall(game);
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_ESCAPE)) {
            game.terminate();
        }
    }

}